﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Views
{
    public partial class ListContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ShowContact();
        }
        private void ShowContact()
        {
            var listContact = new List<Contact>();
            var biz = new ContactBiz();
            if (cbInActive.Checked == true)
            {
                listContact = biz.getAllContactByKey(string.Empty, string.Empty, false);
                if (listContact != null)
                {
                    GVSearchContact.DataSource = listContact;
                    DataBind();
                }
            }
            else
            {
                listContact = biz.getAllContactByKey(string.Empty, string.Empty, true);
                if (listContact != null)
                {
                    GVSearchContact.DataSource = listContact;
                    DataBind();
                }
            }
            
        }
        protected void ChkStatusOnCheckedChanged(object sender, EventArgs e)
        {
           
        }
        protected void GVSearchContact_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVSearchContact.PageIndex = e.NewPageIndex;
            ShowContact();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            var listContact = new List<Contact>();
            var find = new ContactBiz();
            if (cbInActive.Checked == true)
            {
                listContact = find.getAllContactByKey(txtFirstName.Text.ToString(), txtSurName.Text.ToString(), false);
                GVSearchContact.DataSource = listContact;
                DataBind();
            }
            else
            {
                listContact = find.getAllContactByKey(txtFirstName.Text.ToString(), txtSurName.Text.ToString(), true);
                GVSearchContact.DataSource = listContact;
                DataBind();
            }
            //listContact = find.getAllContactByKey(txtFirstName.Text.ToString(), txtSurName.Text.ToString());
            if (listContact != null)
            {
                GVSearchContact.DataSource = listContact;
                DataBind();
            }
        }

        protected void GVSearchContact_RowCommand(object sender, GridViewCommandEventArgs e)
        {           
            if (e.CommandName == "Update")
            {
                Response.Redirect("../Forms/AddContact.aspx?update=ok&ContactId=" + e.CommandArgument);
            }  
        }

        protected void btCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Forms/AddContact.aspx?add=ok");
        }
    }
}